#ifndef LXCUTILS_H
#define LXCUTILS_H

#include <QProcess>
#include <QString>
#include <QStringList>

#define LXC_PROCESS_RUN(x)\
    static LXCProcesResult lxc_##x(QStringList arguments = QStringList()) { \
        return lxc("lxc-" #x, arguments); \
    }\
    static LXCProcesResult lxc_##x(QString argument = QString()) { \
        return lxc("lxc-" #x, QStringList() << argument); \
    }\

class LXCProcesResult
{
public:
    LXCProcesResult(QProcess &process) {
        m_stdout = process.readAllStandardOutput();
        m_stderr = process.readAllStandardError();
    }

    QString getStdout(){
        return m_stdout;
    }
    QString getStderr() {
        return m_stderr;
    }
    QString getFull() {
        return m_stdout + "\n" + m_stderr;
    }

private:
    QString m_stdout;
    QString m_stderr;
};


class LXCProcess
{
public:
    LXC_PROCESS_RUN(attach);
    LXC_PROCESS_RUN(autostart);
    LXC_PROCESS_RUN(cgroup);
    LXC_PROCESS_RUN(checkconfig);
    LXC_PROCESS_RUN(checkpoint);
    LXC_PROCESS_RUN(config);
    LXC_PROCESS_RUN(console);
    LXC_PROCESS_RUN(copy);
    LXC_PROCESS_RUN(create);
    LXC_PROCESS_RUN(destroy);
    LXC_PROCESS_RUN(device);
    LXC_PROCESS_RUN(execute);
    LXC_PROCESS_RUN(freeze);
    LXC_PROCESS_RUN(info);
    LXC_PROCESS_RUN(ls);
    LXC_PROCESS_RUN(monitor);
    LXC_PROCESS_RUN(snapshot);
    LXC_PROCESS_RUN(start);
    LXC_PROCESS_RUN(stop);
    LXC_PROCESS_RUN(unfreeze);
    LXC_PROCESS_RUN(unshare);
    static LXCProcesResult lxc_update_config(QStringList arguments = QStringList()) {
        return lxc("lxc-update-config", arguments);
    };
    LXC_PROCESS_RUN(usersexec);
    LXC_PROCESS_RUN(wait);

    static LXCProcesResult lxc(QString command, QStringList arguments)
    {
        QProcess process;
        process.start(command, arguments);
        process.waitForStarted();
        process.waitForFinished();
        return LXCProcesResult(process);
    }
};

class LXCUtils
{
public:
    static QStringList GetLXCContainers();

    static QStringList GetLxcTemplates();
};

#endif // LXCUTILS_H
